package com.csdj.t309springboot.controller;
import com.csdj.t309springboot.common.CacheConstant;
import com.csdj.t309springboot.common.WebResult;
import com.csdj.t309springboot.common.WebResultMsgCode;
import com.csdj.t309springboot.service.RedisService;
import com.csdj.t309springboot.utils.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 发送邮件控制器
 */
@RestController
@RequestMapping("/mail")
public class MailController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
//    @Autowired
    @Resource
    private JavaMailSender mailSender;
    @Resource
    private RedisService redisService;
    @Value("${spring.mail.sender}")
    private String emailSender;

    @GetMapping("/send")
    public WebResult sendMail(@RequestParam(value = "email",required = true) String email)
            throws MessagingException {
        log.info("email="+ email);
        String checkCode = RandomUtil.getCheckCode();
        //存到redis里面, 并设置过期时间为 10分钟
        redisService.setString(CacheConstant.CACHE_CHECK_CODE+email,checkCode,60*15);
        log.info("redis checkCode="+redisService.getString(CacheConstant.CACHE_CHECK_CODE+email));
//        log.info("验证码是:"+checkCode);
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage,true);
        //发送方的邮箱
        log.info("emailSender="+emailSender);
        mmh.setFrom( emailSender );
        //接收方的邮箱
        mmh.setTo(email.trim());//去除两端空格
        mmh.setSubject("欢迎注册拼图app");
        mmh.setText("<!DOCTYPE html>\n" +
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<meta charset=\"utf-8\">\n" +
                "\t\t<title></title>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<p>您的验证码是:</p>\n" +
                "\t\t<p>"+checkCode+"</p>\n" +
                "\t\t<p>验证码的有效期为一小时</p>\n" +
                "\t\t<p>如非本人操作, 请忽略</p>\n" +
                "\t</body>\n" +
                "</html>",true);
        try {
            mailSender.send(mimeMessage);
            log.info("邮件已发送。");
            return new WebResult(WebResultMsgCode.SUCCESS,"success");
        } catch (Exception e) {
            log.error("发送邮件时发生异常了！", e);
            return new WebResult(WebResultMsgCode.FAIL);
        }
    }


    public static void main(String[] args) {
//        System.out.println("验证码:"+RandomUtil.getCheckCode());;
        System.out.println("邀请码:"+RandomUtil.getInvitationCode());;
    }

}
